/* rqgrsxr.p - GRS Requisitions Header Record ROP                             */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.9 $                                                           */
/*                                                                            */
/* This routine provides the procedures which are executed as a result of the */
/* call from the appropriate Data Type Controllers / structured programs      */
/* It contains business logic pertaining to Global Requisitions               */
/*                                                                            */
/* Revision: 1.1  BY: Bill Pedersen               DATE: 04/25/00  ECO: *N059* */
/* Revision: 1.3  BY: Julie Milligan              DATE: 08/23/00  ECO: *N0N2* */
/* Revision: 1.7  BY: Julie Milligan              DATE: 10/10/00  ECO: *N0S0* */
/* Revision: 1.8  BY: Larry Leeth                 DATE: 12/01/00  ECO: *N0V1* */
/* $Revision: 1.9 $   BY: Nikita Joshi                DATE: 05/04/01  ECO: *M16J* */
/*                                                                            */
/*V8:ConvertMode=NoConvert                                                    */

/* ========================================================================== */
/* ******************************* DEFINITIONS ****************************** */
/* ========================================================================== */

/* STANDARD INCLUDE FILES */
{mfdeclre.i}
{pxmaint.i}
{rqconst.i}     /* GRS CONSTANTS DEFINITIONS */
{rqalttd.i new} /* GRS APPROVER TEMP-TABLE DEFINITIONS */


/* Define Handles for the programs. */
{pxphdef.i pxgblmgr}
/* End Define Handles for the programs. */

define temp-table ttRqm_mstr like rqm_mstr.

/* ========================================================================== */
/* ******************************** FUNCTIONS ******************************* */
/* ========================================================================== */

/*============================================================================*/
FUNCTION isGRSInUse RETURNS logical :
/*------------------------------------------------------------------------------
Purpose:       Determines if Global Requisition System is activiated in the
               control file.
Exceptions:    None
Conditions:
        Pre:   None
       Post:   mfc_ctrl(r)
Notes:
History:       Extracted from popomtea.p
------------------------------------------------------------------------------*/
   define variable returnData as logical no-undo.

   {gpfilev.i}
   {gpfile.i &file_name="'rqf_ctrl'"}

   if can-find (mfc_ctrl where mfc_field = "grs_installed" and
                   mfc_logical = yes) and
      file_found then
      returnData = true.
   else
      returnData = false.

   return (returnData).
END FUNCTION.


/*============================================================================*/
FUNCTION isToleranceCheckingActive returns logical:
/*------------------------------------------------------------------------------
Purpose:       Determines GRS Out of Tolerance Checking is active for
               Requisitions
Exceptions:    None
Conditions:
   Pre:        None
   Post:       None
Notes:
History:       Procedure initially extracted from popomtra.p
------------------------------------------------------------------------------*/
   define variable returnData as logical initial false no-undo.
   define buffer rqf_ctrl for rqf_ctrl.

   {pxrun.i &PROC='readGRSRequisitionControl'
            &PARAM="(buffer rqf_ctrl)"
            &NOAPPERROR=TRUE
            &CATCHERROR=TRUE}

   if return-value = {&SUCCESS-RESULT} then
      returnData = (rqf_use_tolpct or rqf_use_tolval).

   return (returnData).
END FUNCTION.


/* ========================================================================== */
/* ******************************* MAIN BLOCK ******************************* */
/* ========================================================================== */

/* ========================================================================== */
/* *************************** INTERNAL PROCEDURES ************************** */
/* ========================================================================== */

/*============================================================================*/
PROCEDURE closeRequisition :
/*------------------------------------------------------------------------------
Purpose:       Closes a requisition line.
Exceptions:    NONE
Conditions:
        Pre:   None
        Post:  None
Notes:
History:       Extracted from popomtd.p
------------------------------------------------------------------------------*/
   define input parameter pPOLineStatus             as character no-undo.
   define input parameter pRequisitionId            as character no-undo.
   define input parameter pRequisitionLineId        as integer no-undo.
   define input parameter pCloseRequisitionResponse as logical no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pPOLineStatus = 'C'
         and ({pxfunct.i &FUNCTION='isGRSInUse' &PROGRAM='rqgrsxr.p'})
      then do:
            {gprunmo.i &program = "popocls.p"
                       &module = "GRS"
                       &param = """(input pRequisitionId,
                                    input pRequisitionLineId,
                                    input pCloseRequisitionResponse)"""}
      end.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.


/*============================================================================*/
PROCEDURE getRouteToApprover :
/*------------------------------------------------------------------------------
Purpose:       For an Out of Tolerance requisition, get the next approver.
Exceptions:    NONE
Conditions:
        Pre:   rqm_mstr(r)
        Post:  rqf_ctrl(r), rqm_mstr(r), rqda_det(r)
Notes:
History:       Extracted from popomtd.p
------------------------------------------------------------------------------*/
   define input  parameter pReqNumber as character no-undo.
   define output parameter pRouteToApprover as character no-undo.

   define buffer rqf_ctrl for rqf_ctrl.
   define buffer rqm_mstr for rqm_mstr.
   define buffer rqda_det for rqda_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      /*DETERMINE THE ROUTE-TO TO BE USED BY RQRTMT.P AND STASH IT*/
      {pxrun.i &PROC='readGRSRequisitionControl'
               &PARAM="(buffer rqf_ctrl)"}

      if return-value = {&SUCCESS-RESULT} then do:
         {pxrun.i &PROC='processRead'
                  &PARAM="(input pReqNumber,
                           buffer rqm_mstr,
                           input {&NO_LOCK_FLAG},
                           input {&NO_WAIT_FLAG})"}

         if return-value = {&SUCCESS-RESULT} then do:

            if rqf_oot_rtg = OOT_RTG_REQUESTER then do:
               pRouteToApprover = rqm_rqby_userid.
            end.
            else if rqf_oot_rtg = OOT_RTG_FIRST_APPROVER then do:
               for each rqda_det no-lock
               where rqda_nbr = rqm_nbr
               by rqda_date by rqda_time:
                  pRouteToApprover = rqda_apr_userid.
                  leave.
               end.
            end.
            else if rqf_oot_rtg = OOT_RTG_LAST_APPROVER then do:
               for each rqda_det no-lock
               where rqda_nbr = rqm_nbr
               by rqda_date descending by rqda_time descending:

                  pRouteToApprover = rqda_apr_userid.
                  leave.
               end.
            end.
         end.
         else
            return return-value.
      end.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.


/*============================================================================*/
PROCEDURE processRead :
/*------------------------------------------------------------------------------
Purpose:       Locate and bring the GRS Requisition Master record into scope.
Exceptions:    RECORD-LOCKED, RECORD-NOT-FOUND
Conditions:
        Pre:   None
        Post:  rqm_mstr(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.
   define parameter buffer rqm_mstr for rqm_mstr.
   define input parameter pLockFlag as logical no-undo.
   define input parameter pWaitFlag as logical no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pLockFlag then do:
         if pWaitFlag then do:
            for first rqm_mstr
               where rqm_nbr = pReqNumber
               exclusive-lock:
            end.
         end.
         else do:
            find rqm_mstr
               where rqm_nbr = pReqNumber
               exclusive-lock no-error no-wait.

            if locked rqm_mstr then return {&RECORD-LOCKED}.
         end.
      end.
      else do:
         for first rqm_mstr where rqm_nbr = pReqNumber no-lock:
         end.
      end.

      if not available rqm_mstr then return {&RECORD-NOT-FOUND}.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.

/*============================================================================*/
PROCEDURE processReadReturnTempTable :
/*------------------------------------------------------------------------------
Purpose:       Locate and bring the GRS Requisition Master record into scope.
Exceptions:    RECORD-LOCKED, RECORD-NOT-FOUND
Conditions:
        Pre:   None
        Post:  rqm_mstr(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pRequisitonId as character no-undo.
   define parameter buffer ttRqm_mstr for ttRqm_mstr.
   define input parameter pLockFlag as logical no-undo.
   define input parameter pWaitFlag as logical no-undo.

   define buffer rqm_mstr for rqm_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
         {pxrun.i &PROC='processRead'
            &PARAM="(input pRequisitonId,
                     buffer rqm_mstr,
                     input pLockFlag,
                     input pWaitFlag)"}

         if return-value <> {&SUCCESS-RESULT} then
            return error return-value.

         buffer-copy rqm_mstr to ttRqm_mstr.

   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.



/*============================================================================*/
PROCEDURE readGRSRequisitionControl :
/*------------------------------------------------------------------------------
Purpose:       Read the GRS Requisition Control file into scope
Exceptions:    {&APP-ERROR-RESULT}
Conditions:
        Pre:   None
       Post:   rqf_ctrl(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define parameter buffer rqf_ctrl for rqf_ctrl.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      for first rqf_ctrl no-lock:
      end.

      if not available rqf_ctrl then do:
         /*594-CONTROL FILE ERROR.CHECK APPLICABLE CONTROL FILES FOR EXISTENCE*/
         {pxmsg.i
            &MSGNUM=594
            &ERRORLEVEL={&APP-ERROR-NO-REENTER-RESULT}}

         /*291 - NO CONTROL FILE RECORD FOUND FOR rqf_ctrl*/
         {pxmsg.i
            &MSGNUM=291
            &ERRORLEVEL={&APP-ERROR-NO-REENTER-RESULT}
            &MSGARG1='rqf_ctrl'}
         return error {&APP-ERROR-RESULT}.
      end.

   end.
   return {&SUCCESS-RESULT}.

END PROCEDURE.


/*============================================================================*/
PROCEDURE reOpenRequisition :
/*------------------------------------------------------------------------------
Purpose:       Re-Open a Requisition Line
Exceptions:    NONE
Conditions:
        Pre:   rqd_det(r), rqm_mstr(r)
        Post:  rqd_det(w), rqm_mstr(w)
Notes:
History:       Extracted from popomth.p
------------------------------------------------------------------------------*/
   define input parameter pRequisitionId as character no-undo.
   define input parameter pRequisitionLineId as integer no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if ({pxfunct.i &FUNCTION='isGRSInUse' &PROGRAM='rqgrsxr.p'})
      then do:
         {gprunmo.i &program = "poreopn.p" &module = "GRS"
                    &param = """(input pRequisitionId,
                                 input pRequisitionLineId)"""}
      end. /* IF ({pxfunct.i &FUNCTION='isGRSInUse'....*/
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.


/*============================================================================*/
PROCEDURE reverseRouteRequisitionTo :
/*------------------------------------------------------------------------------
Purpose:       Restores the previous requisition "routed to".  This allows
               the user to undo a route-to in the event it needs to be undone.
Exceptions:    None
Conditions:
        Pre:   rqm_mstr(r)
        Post:  rqm_mstr(w)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.

   define buffer rqm_mstr for rqm_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      {pxrun.i &PROC='processRead'
               &PARAM="(input pReqNumber,
                        buffer rqm_mstr,
                        input {&LOCK_FLAG},
                        input {&NO_WAIT_FLAG})"}

      if return-value = {&SUCCESS-RESULT} then
         assign
            rqm_rtdto_purch = rqm_prev_rtp
            rqm_rtto_userid = rqm_prev_userid
            rqm_pent_userid = ""
            rqm_prev_userid = "".
      else
         return return-value.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.


/*============================================================================*/
PROCEDURE routeRequisitionTo :
/*------------------------------------------------------------------------------
Purpose:       Updates the requisition with the route-to selected
Exceptions:    None
Conditions:
        Pre:   rqm_mstr(r)
        Post:  rqm_mstr(w)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.
   define input parameter pRouteToPurchasing as logical no-undo.
   define input parameter pRoutedByUserID as character no-undo.
   define input parameter pRouteToUserID as character no-undo.

   define buffer rqm_mstr for rqm_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      {pxrun.i &PROC='processRead'
               &PARAM="(input pReqNumber,
                        buffer rqm_mstr,
                        input {&LOCK_FLAG},
                        input {&NO_WAIT_FLAG})"}

      if return-value = {&SUCCESS-RESULT} then
         assign
            rqm_prev_rtp = rqm_rtdto_purch
            rqm_rtdto_purch = pRouteToPurchasing
            rqm_pent_userid = pRoutedByUserID
            rqm_prev_userid = rqm_rtto_userid
            rqm_rtto_userid = pRouteToUserID.
      else
         return return-value.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.


/*============================================================================*/
PROCEDURE setRequisitionBuyer :
/*------------------------------------------------------------------------------
Purpose:       Assigns a userid to the requisition buyer field
Exceptions:    NOne
Conditions:
        Pre:   rqm_mstr(r)
        Post:  rqm_mstr(w)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.
   define input parameter pBuyer as character no-undo.

   define buffer rqm_mstr for rqm_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      {pxrun.i &PROC='processRead'
               &PARAM="(input pReqNumber,
                        buffer rqm_mstr,
                        input {&LOCK_FLAG},
                        input {&NO_WAIT_FLAG})"}

      if return-value = {&SUCCESS-RESULT} then
         rqm_buyer = pBuyer.
      else
         return return-value.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.


/*============================================================================*/
PROCEDURE setRequisitionStatusToApproved :
/*------------------------------------------------------------------------------
Purpose:       Sets the requisition status to approved
Exceptions:    None
Conditions:
        Pre:   rqm_mstr(r), rqd_det(r)
        Post:  rqm_mstr(w), rqd_det(w)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.

   define buffer rqm_mstr for rqm_mstr.
   define buffer rqd_det for rqd_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      for each rqd_det exclusive-lock where rqd_nbr = pReqNumber:
         rqd_aprv_stat = APPROVAL_STATUS_APPROVED.
      end.

      {pxrun.i &PROC='processRead'
               &PARAM="(input pReqNumber,
                        buffer rqm_mstr,
                        input {&LOCK_FLAG},
                        input {&NO_WAIT_FLAG})"}

      if return-value = {&SUCCESS-RESULT} then
         rqm_aprv_stat = APPROVAL_STATUS_APPROVED.
      else
         return return-value.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.

/*============================================================================*/
PROCEDURE routeOutOfToleranceReq :
/*------------------------------------------------------------------------------
<Comment1>
rqgrsxr.p
 routeOutOfToleranceReq(
   input character pRequisitionId,
   input character pLastApprover,
   input character pNewRouteTo,
   input character pDefaultBuyer)

Parameters:
   pRequisitionId - GRS Requisition ID
   pLastApprover  - Last person that approved this requisition
   pNewRouteTo    - New Route To Approver as entered by the user
   pDefaultBuyer  - Original Buyer for requisition

Exceptions: APP-ERROR-RESULT

PreConditions: Create a Purchase Order Line and Mark it out of Tolerance.
               The switch to the req database must have occurred prior
               calling this procedure.

PostConditions: Requisition will have email acknowledments
                sent and Requisition History will be tracked based
                on the new route to approver and buyer as entered
                by the user.

</Comment1>
<Comment2>
Notes:

</Comment2>
<Comment3>
History:

</Comment3>
------------------------------------------------------------------------------*/
   define input parameter pRequisitionId        as character no-undo.
   define input parameter pLastApprover         as character no-undo.
   define input parameter pNewRouteTo           as character no-undo.
   define input parameter pDefaultBuyer         as character no-undo.

   define variable emailSentTo                  as character no-undo.
   define variable globalUserId                 as character no-undo.
   define variable routeTo                      as character no-undo.

   define buffer rqm_mstr for rqm_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      {pxgetph.i pxgblmgr}
      globalUserId =
         {pxfunct.i &FUNCTION='getCharacterValue' &PROGRAM='pxgblmgr.p'
                    &HANDLE=ph_pxgblmgr
                    &PARAM="input 'global_userid'"}.

      /* LOCK RQM_MSTR SO YOU CAN CHANGE THE STATUS */
      /* GET REQUISITION MASTER IN SCOPE           */
      {pxrun.i &PROC='processRead'
               &PARAM="(input pRequisitionId,
                  buffer rqm_mstr,
                  input {&LOCK_FLAG},
                  input {&NO_WAIT_FLAG})"}

      if return-value <> {&SUCCESS-RESULT} then do:
         return error return-value.
      end.

      rqm_mstr.rqm_aprv_stat = APPROVAL_STATUS_OOT.

      /* IF THE USER PROCESSING THIS MARK OUT OF TOLERANCE MADE BOTH  */
      /* THE NEW ROUTE TO AND NEW BUYER = BLANK, THEN THIS MEANS THE  */
      /* USER DOES NOT WANT TO RE-ROUTE THIS REQUISITION.             */
      /* KEEP THE REQUISITION ROUTED TO THE ORIGINAL BUYER.           */
      if pNewRouteTo = "" and rqm_mstr.rqm_buyer = "" then do:
         {pxrun.i &PROC='routeRequisitionTo'
             &PARAM="(input pRequisitionId,
                   input no,    /* Route To Purchasing */
                   input globalUserId,
                   input pDefaultBuyer)"}
         return {&SUCCESS-RESULT}.
      end.

      /* IF THE USER PROCESSING THIS MARK OUT OF TOLERANCE IS NOT THE */
      /* DEFAULT BUYER, THEN THAT USER IS NOT ALLOWED                 */
      /* TO RE-ROUTE THIS REQUISITION.                                */
      /* KEEP THE REQUISITION ROUTED TO THE ORIGINAL BUYER.           */
      if (pDefaultBuyer <> globalUserId)
      then do:
         {pxrun.i &PROC='routeRequisitionTo'
             &PARAM="(input pRequisitionId,
                   input no,    /* Route To Purchasing */
                   input globalUserId,
                   input pDefaultBuyer)"}

         return {&SUCCESS-RESULT}.
      end.

      if (pNewRouteTo <> pLastApprover and pNewRouteTo <> "") or
         (rqm_mstr.rqm_buyer = pDefaultBuyer) or
         (rqm_mstr.rqm_buyer = "")
      then routeTo = pNewRouteTo.

      else routeTo = rqm_mstr.rqm_buyer.

      if (routeTo = "" and pNewRouteTo = "")
         then routeTo = pLastApprover.

      {pxrun.i &PROC='routeRequisitionTo'
          &PARAM="(input rqm_nbr,
                   input no,    /* Route To Purchasing */
                   input globalUserId,
                   input routeTo)"}

      /*SEND EMAILS*/
      {gprun.i ""rqemsend.p""
         "(input recid(rqm_mstr),
           input ACTION_ROUTE,
           output emailSentTo)"}

      /*WRITE HISTORY RECORD*/
      {gprun.i ""rqwrthst.p""
         "(input rqm_mstr.rqm_nbr,
           input 0,
           input ACTION_ROUTE,
           input rqm_mstr.rqm_rtto_userid,
           input rqm_mstr.rqm_rtto_userid,
           input emailSentTo)"}

   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.


/*============================================================================*/
PROCEDURE validateAlternateApprover :
/*------------------------------------------------------------------------------
Purpose:       Validates that approver (pSessionUserID) who is trying to
               approve a req on behalf of someone else (pCurrentlyRoutedTo)
               is a validate alternate approver.  This happens when the user
               indicates another user than him/herself in the "Currently
               Routed To" field.
Exceptions:    APP-ERROR-RESULT
Conditions:
        Pre:   rqm_mstr(r)
        Post:  rqm_mstr(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.
   define input parameter pCurrentlyRoutedTo as character no-undo.
   define input parameter pSessionUserID as character no-undo.

   define variable alternate_ok as logical no-undo.
   define buffer rqm_mstr for rqm_mstr.
   define buffer rqa_mstr for rqa_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pCurrentlyRoutedTo <> pSessionUserID then do:

         /*BUILD APPROVER LIST*/
         {pxrun.i &PROC='processRead'
                  &PARAM="(input pReqNumber,
                           buffer rqm_mstr,
                           input {&NO_LOCK_FLAG},
                           input {&NO_WAIT_FLAG})"}

         if return-value = {&SUCCESS-RESULT} then do:
            {gprun.i ""rqbal.p"" "(input recid(rqm_mstr))"}
         end.
         else
            return return-value.

         /*CHECK IF VALID ALTERNATE APPROVER*/
         {gprun.i ""rqckalt.p""
            "(input pCurrentlyRoutedTo,
              input pSessionUserID,
              output alternate_ok)"}

         if not alternate_ok then do:
            for each rqa_mstr where rqa_apr = pCurrentlyRoutedTo no-lock:
               if pSessionUserID = rqa_alt_apr[1]
               or pSessionUserID = rqa_alt_apr[2]
               or pSessionUserID = rqa_admin_apr
               then do:
                  alternate_ok = true.
                  leave.
               end.
            end.
         end.

         if not alternate_ok then do:
            /*YOU ARE NOT AN ALTERNATE FOR THE CURRENT ROUTE-TO*/
            {pxmsg.i
               &MSGNUM=2112
               &ERRORLEVEL={&APP-ERROR-RESULT}}
            return error {&APP-ERROR-RESULT}.
         end.
      end.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.


/*============================================================================*/
PROCEDURE validateBuyer :
/*------------------------------------------------------------------------------
Purpose:       Validates that a userid is a valid buyer
Exceptions:    APP-ERROR-RESULT
Conditions:
        Pre:   None
       Post:   usr_mstr(r), rqr_mstr(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pBuyer as character no-undo.

   define buffer usr_mstr for usr_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      if not can-find(first usr_mstr where usr_userid = pBuyer) then do:
         /*UNAVAILABLE USER*/
         {pxmsg.i
            &MSGNUM=2054
            &ERRORLEVEL={&APP-ERROR-RESULT}
            &FIELDNAME='rqm_buyer'}

         return error {&APP-ERROR-RESULT}.
      end.

      if not can-find(rqr_mstr where rqr_role = BUYER and rqr_userid = pBuyer)
      then do:
         /*NOT A VALID BUYER*/
         {pxmsg.i
            &MSGNUM=2091
            &ERRORLEVEL={&APP-ERROR-RESULT}
            &FIELDNAME='rqm_buyer'}

         return error {&APP-ERROR-RESULT}.
      end.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.



/*============================================================================*/
PROCEDURE validateCurrentlyRoutedTo :
/*------------------------------------------------------------------------------
Purpose:    For route action, validates that the entered "Currently Routed To"
            is the same as the id that the req is currently routed to; also
            validates that  if the req is routed to purchasing, that the userid
            doing it is not an alternate  (userid must be the route-to assigned
            to the req).

            For  reverse  route  action, validates that the "Currently Routed
            To" is the same as the id as the person who last approved the req
            and that either the session user is what is entered in  "Currently
            Routed  To" or was the "alternate" who last routed the req.

Exceptions: APP-ERROR-RESULT
Conditions:
        Pre:  rqm_mstr(r)
        Post: rqm_mstr(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.
   define input parameter pSessionUserID as character no-undo.
   define input parameter pRouteAction as character no-undo.
   define input parameter pCurrentlyRoutedTo as character no-undo.

   define buffer rqm_mstr for rqm_mstr.


   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      {pxrun.i &PROC='processRead'
               &PARAM="(input pReqNumber,
                        buffer rqm_mstr,
                        input {&NO_LOCK_FLAG},
                        input {&NO_WAIT_FLAG})"}

      if return-value = {&SUCCESS-RESULT} then do:
         if pRouteAction = ACTION_ROUTE then do:

            if pCurrentlyRoutedTo <> rqm_rtto_userid then do:
               /*REQUISITION CURRENTLY ROUTED TO*/
               {pxmsg.i
                  &MSGNUM=2094
                  &ERRORLEVEL={&APP-ERROR-RESULT}
                  &MSGARG1=rqm_rtto_userid}

               return error {&APP-ERROR-RESULT}.
            end.

            if  rqm_rtdto_purch
            and
            (
               rqm_rtto_userid <> pSessionUserID
               or
               pCurrentlyRoutedTo <> pSessionUserID
            )
            then do:
               /*ALTERNATES NOT ALLOWED FOR BUYERS*/
               {pxmsg.i
                  &MSGNUM=2074
                  &ERRORLEVEL={&APP-ERROR-RESULT}}
               return error {&APP-ERROR-RESULT}.
            end.
         end.
         else do:
            if not
            (
               rqm_prev_userid = pCurrentlyRoutedTo
               and
               (
                  /*THIS PERSON IS THE LAST ONE WHO ROUTED  THE  REQ
                  EVEN THOUGH IT MAY HAVE BEEN BY  SOMEONE  ELSE  ON
                  THEIR BEHALF (ALTERNATE/ADMIN)*/
                  pSessionUserID = pCurrentlyRoutedTo

                  or

                  /*THIS PERSON IS THE LAST ONE TO REROUTE ON BEHALF
                  OF SOMEONE ELSE (ALTERNATE/ADMIN)*/
                  pSessionUserID = rqm_pent_userid
               )
            )
            then do:
               /*YOU WERE NOT THE LAST TO ROUTE THIS REQUISITION*/
               {pxmsg.i
                  &MSGNUM=2111
                  &ERRORLEVEL={&APP-ERROR-RESULT}}
               return error {&APP-ERROR-RESULT}.
            end.
         end.
      end.
      else
         return return-value.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.


/*============================================================================*/
PROCEDURE validateReqIsOpen :
/*------------------------------------------------------------------------------
Purpose:       Ensure the requisition selected is open. A closed requisition
               cannot be used on a PO.
Exceptions:    APP-ERROR-RESULT
Conditions:
        Pre:   None
        Post:  rqm_mstr(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.

   define buffer rqm_mstr for rqm_mstr.


   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      {pxrun.i &PROC='processRead'
               &PARAM="(input pReqNumber,
                        buffer rqm_mstr,
                        input {&NO_LOCK_FLAG},
                        input {&NO_WAIT_FLAG})"}

      if return-value <> {&SUCCESS-RESULT} then
         return return-value.

      if rqm_status <> "" then do:
         if rqm_status = "C" then do:
            /* MESSAGE #3325 - REQUISITION IS CLOSED */
            {pxmsg.i
               &MSGNUM=3325
               &ERRORLEVEL={&APP-ERROR-RESULT}}
            return error {&APP-ERROR-RESULT}.
         end.
         else do:
            /* MESSAGE #2101 - REQUISITION IS CANCELLED */
            {pxmsg.i
               &MSGNUM=2101
               &ERRORLEVEL={&APP-ERROR-RESULT}}
            return error {&APP-ERROR-RESULT}.
         end.
      end.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.



/*============================================================================*/
PROCEDURE validateReqRoutedToPurchasing :
/*------------------------------------------------------------------------------
Purpose:       Ensure the the requisition has been routed to Purchasing before
               it can be used  by a PO
Exceptions:    APP-ERROR-RESULT
Conditions:
        Pre:   None
        Post:  rqm_mstr(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if not can-find(first rqm_mstr where rqm_nbr = pReqNumber
         and rqm_rtdto_purch = TRUE)
      then do:

         /*REQUISITION HAS NOT BEEN ROUTED TO PURCHASING*/
         {pxmsg.i
            &MSGNUM=2099
            &ERRORLEVEL={&APP-ERROR-RESULT}}
         return error {&APP-ERROR-RESULT}.
      end.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.


/*============================================================================*/
PROCEDURE validateRouteToUser :
/*------------------------------------------------------------------------------
Purpose:       Validates that the route to userid is a valid user, nonblank,
               and is a valid approver for the requisition
Exceptions:    APP-ERROR-RESULT
Conditions:
        Pre:   None
       Post:   usr_mstr(r), rqm_mstr(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqNumber as character no-undo.
   define input parameter pRouteTo as character no-undo.

   define buffer rqm_mstr for rqm_mstr.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      /*MUST BE A VALID USER IN USR_MSTR*/
      if not can-find(usr_mstr where usr_userid = pRouteTo) then do:

         /*UNAVAILABLE USER*/
         {pxmsg.i
            &MSGNUM=2054
            &ERRORLEVEL={&APP-ERROR-RESULT}
            &FIELDNAME='routeToApprover'}

         return error {&APP-ERROR-RESULT}.
      end.


      /*MUST BE NONBLANK*/
      if pRouteTo = "" then do:

         /*BLANK NOT ALLOWED*/
         {pxmsg.i
            &MSGNUM=40
            &ERRORLEVEL={&APP-ERROR-RESULT}
            &FIELDNAME='routeToApprover'}

         return error {&APP-ERROR-RESULT}.
      end.

      /*BUILD APPROVER LIST*/
      {pxrun.i &PROC='processRead'
               &PARAM="(input pReqNumber,
                        buffer rqm_mstr,
                        input {&NO_LOCK_FLAG},
                        input {&NO_WAIT_FLAG})"}

      if return-value = {&SUCCESS-RESULT} then do:
         {gprun.i ""rqbal.p"" "(input recid(rqm_mstr))"}

         /*MUST BE A VALID APPROVER FOR THE REQ*/
         find ttcal where ttcal_apr_userid = pRouteTo no-lock no-error.

         if not available ttcal then do:
            /*ROUTE TO IS NOT IN APPROVER LIST*/
            {pxmsg.i
               &MSGNUM=2097
               &ERRORLEVEL={&WARNING-RESULT}
               &FIELDNAME='routeToApprover'}

            return {&WARNING-RESULT}.
         end.
      end.
      else
         return return-value.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.


/*============================================================================*/
PROCEDURE validateRequisitionExchangeRate :
/*------------------------------------------------------------------------------
Purpose:       Ensure that the exchange rate can be converted to the exchange
               rate cited by the PO
Exceptions:    APP-ERROR-RESULT
Conditions:
        Pre:   None
        Post:  None
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pReqCurrencyId as character no-undo.
   define input parameter pForeignCurrencyId as character no-undo.
   define input parameter pRateType as character no-undo.

   define variable exchangeRate1 as decimal no-undo.
   define variable exchangeRate2 as decimal no-undo.
   define variable errorCode as integer no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      {gprun.i 'rqexrt.p'
         "(input pReqCurrencyId,
           input pForeignCurrencyId,
           input pRateType,
           output exchangeRate1,
           output exchangeRate2,
           output errorCode)"}
      if errorCode <> 0 then do:

         /*NO EXCHANGE RATE FOR REQUISITION TO PO CURRENCY*/
         {pxmsg.i &MSGNUM=2120 &ERRORLEVEL={&APP-ERROR-RESULT}}
         return error {&APP-ERROR-RESULT}.

      end.
   end.

   return {&SUCCESS-RESULT}.
END PROCEDURE.
